บทนำสู่ PyTorch: ทำไมเทนเซอร์จึงสำคัญ
PyTorch เป็นเฟรมเวิร์กโอเพนซอร์สที่ยืดหยุ่นและมีความไดนามิกสูง ซึ่งได้รับความนิยมในการวิจัยด้านการเรียนรู้เชิงลึกและการพัฒนาแบบรวดเร็ว ที่แก่นกลางของมันคือ เทนเซอร์ เป็นโครงสร้างข้อมูลที่จำเป็นอย่างยิ่ง มันคืออาร์เรย์หลายมิติที่ออกแบบมาเพื่อจัดการกับการทำงานทางตัวเลขที่จำเป็นสำหรับโมเดลการเรียนรู้เชิงลึกได้อย่างมีประสิทธิภาพ โดยรองรับการเร่งความเร็วด้วย การเร่งความเร็วด้วย GPU automatically.
1. ทำความเข้าใจโครงสร้างของเทนเซอร์
ทุกข้อมูลนำเข้า ผลลัพธ์ และพารามิเตอร์ของโมเดลใน PyTorch ถูกห่อหุ้มไว้ในเทนเซอร์ พวกเขาทำหน้าที่เหมือนอาร์เรย์ของ NumPy แต่ได้รับการปรับให้เหมาะสมกับการประมวลผลบนฮาร์ดแวร์เฉพาะ เช่น GPUทำให้มีประสิทธิภาพมากกว่าในการดำเนินการทางพีชคณิตเชิงเส้นขนาดใหญ่ที่จำเป็นสำหรับเครือข่ายประสาทเทียม
คุณสมบัติหลักที่กำหนดเทนเซอร์มีดังนี้:
- รูปร่าง: กำหนดมิติของข้อมูล แสดงเป็นลำดับ (เช่น $4 \times 32 \times 32$ สำหรับชุดภาพ)
- ชนิดข้อมูล: ระบุประเภทตัวเลขขององค์ประกอบที่เก็บ (เช่น
torch.float32สำหรับน้ำหนักของโมเดล,torch.int64สำหรับการดัชนี) - อุปกรณ์: บ่งบอกตำแหน่งฮาร์ดแวร์จริง: โดยทั่วไปแล้วคือ
'cpu'หรือ'cuda'(GPU NVIDIA)
กราฟไดนามิกและระบบอัตโนมัติคำนวณอนุพันธ์ (Autograd)
PyTorch ใช้โมเดลการประมวลผลตามลำดับ หมายความว่ากราฟการคำนวณจะถูกสร้างขึ้นเมื่อปฏิบัติการถูกดำเนินการ ซึ่งทำให้เครื่องมือคำนวณอนุพันธ์อัตโนมัติที่ฝังอยู่ในตัว อย่าง Autogradสามารถติดตามทุกการดำเนินการบนเทนเซอร์ ภายใต้เงื่อนไขที่ตั้งค่าคุณสมบัติ
requires_grad=True ไว้ ทำให้สามารถคำนวณเกรเดียนต์ได้อย่างง่ายดายระหว่างการคำนวณย้อนกลับ (backpropagation)
TERMINALbash — pytorch-env
> Ready. Click "Run" to execute.
>
TENSOR INSPECTOR Live
Run code to inspect active tensors
Question 1
Which command creates a $5 \times 5$ tensor containing random numbers following a uniform distribution between 0 and 1?
Question 2
If tensor $A$ is on the CPU, and tensor $B$ is on the CUDA device, what happens if you try to compute $A + B$?
Question 3
What is the most common data type (dtype) used for model weights and intermediate calculations in Deep Learning?
Challenge: Tensor Manipulation and Shape
Prepare a tensor for a specific matrix operation.
You have a feature vector $F$ of shape $(10,)$. You need to multiply it by a weight matrix $W$ of shape $(10, 5)$. For matrix multiplication (MatMul) to work, $F$ must be 2-dimensional.
Step 1
What should the shape of $F$ be before multiplication with $W$?
Solution:
The inner dimensions must match, so $F$ must be $(1, 10)$. Then $(1, 10) @ (10, 5) \rightarrow (1, 5)$.
Code:
The inner dimensions must match, so $F$ must be $(1, 10)$. Then $(1, 10) @ (10, 5) \rightarrow (1, 5)$.
Code:
F_new = F.unsqueeze(0) or F_new = F.view(1, -1)Step 2
Perform the matrix multiplication between $F_{new}$ and $W$ (shape $(10, 5)$).
Solution:
The operation is straightforward MatMul.
Code:
The operation is straightforward MatMul.
Code:
output = F_new @ W or output = torch.matmul(F_new, W)Step 3
Which method explicitly returns a tensor with the specified dimensions, allowing you to flatten the tensor back to $(50,)$? (Assume $F$ was $(5, 10)$ initially and is now flattened.)
Solution:
Use the
Code:
Use the
view or reshape methods. The fastest way to flatten is often using -1 for one dimension.Code:
F_flat = F.view(-1) or F_flat = F.reshape(50)